//logs.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    webAddr: 'https://www.shangjiatech.com/invoice/web/index.html',
    shouVipMode: 1,
    wechatName: '',
    phone: '',
    vipPhone: '',
    wechatHeadImg: '',
    vipType: '',
    dueTime: ''
  },
  onLoad: function () {
    // 判断是否登录 未登录则跳转登录
    var wxUserId = app.globalData.wxUserId
    if(wxUserId == null || wxUserId == '' || wxUserId == undefined){
      wx.reLaunch({
        url: '/pages/wechat/wechat',
      })
    }
  },
  onShow: function () {
    var that = this
    //添加选中效果
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1 //这个数是，tabBar从左到右的下标，从0开始
      })
    }
    that.initMine()
  },
  onShareAppMessage: function (res) {
    console.log("用户开始分享朋友", res)
    var wxUserId = app.globalData.wxUserId
    return {
      title: "邀请你使用电子发票",
      path:'/pages/index/index?wxUserId=' + wxUserId,
      imageUrl: '/pages/resource/images/share.png',
      success: function (res) {console.log("转发成功", res)},
      fail: function (res) {
        // 转发失败
        console.log("用户点击了取消", res)
      } 
    }
  },
  onShareTimeline: function(res) { //分享朋友圈
    var wxUserId = app.globalData.wxUserId
    console.log("用户开始分享朋友圈", res)
    return {
      title: "邀请你使用电子发票",
      query: "wxUserId=" + wxUserId,
      imageUrl: '/pages/resource/images/share.png',
      success: function (res) {console.log("转发朋友圈成功", res)},
      fail: function (res) {
        // 转发失败
        console.log("用户点击了取消", res)
      } 
    }
  },
  initMine: function(){
    var that = this
    console.log('---------------initMine--------------')
    // 获取用户权限信息，vip请求后台获取vip详细信息，
    // 非vip先从缓存中获取微信信息，缓存为空则请求后台。
    var wxUserType = app.globalData.wxUserType
    var wxUserId = app.globalData.wxUserId
    if(wxUserId == null || wxUserId == ''){ //未登录
      that.setData({
        shouVipMode: 1,
        wechatName: "暂未登录",
        wechatHeadImg: '/pages/resource/images/head.jpg',
      })
    } else {
      app.http.post('/wechat/getMineInfo',{userId: wxUserId}).then(res =>{
        var shouVipMode = 1
        if(wxUserType == '3'){
          // 开启vip信息模块
          shouVipMode = 2 
        }
        if(res.status == '200'){
          var result = res.data
          let dueTime = "已"
          if(wxUserType == '3'){
            let days = that.judgeDateDays(result.userInfo.dueTime)
            if (days > 0) { //会员未过期
              if(days < 30){
               dueTime =  days + "天后"
              } else {
               dueTime =  result.userInfo.dueTime
              }
             }
          }
          that.setData({
            shouVipMode: shouVipMode,
            wechatName: result.wechatInfo.wechatName,
            phone: this.hidePhoneInfo(result.wechatInfo.phone),
            vipPhone: result.wechatInfo.phone,
            wechatHeadImg: result.wechatInfo.wechatHeadImg,
            vipType: result.userInfo.vipType,
            dueTime: dueTime
          })
        }else{
          wx.reLaunch({
            url: '/pages/wechat/wechat',
          })
        }   
      })
    }
  },
  login: function() {
    wx.showModal({
      title: '退出登录',
      content: '是否确认退出登录',
      showCancel: true,//是否显示取消按钮
      cancelColor:'#87ceeb',//取消文字的颜色 在ios中需要使用16进制
      success: function (res) {
        if (!res.cancel) {
           //点击确定
           // 清理缓存
          wx.clearStorageSync()
          app.globalData.wxUserId = ''
          app.globalData.shareId = ''
          app.globalData.wxUserType = ''
          wx.reLaunch({
            url: '/pages/wechat/wechat',
          })
        }
     },
     fail: function (res) { },//接口调用失败的回调函数
    })
  },
  judgeDateDays(dateTime){//判断日期与当前日期的天数差
    // 指定日期和时间
    var EndTime = new Date(dateTime);
    // 当前系统时间
    var NowTime = new Date();
    var t = EndTime.getTime() - NowTime.getTime();
    var d = Math.floor(t / 1000 / 60 / 60 / 24);
    return d;
  },
  updateMailBox: function(){ //修改邮箱
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  goVip: function() {
    // 判断是否登录，未登录则跳转登录页面
    var wxUserId = app.globalData.wxUserId
    if(wxUserId != null && wxUserId != ''){
      if(app.globalData.platform == "ios" && !app.globalData.iosPayStatus){
        wx.showToast({
          title: '由于相关规范，iOS功能暂不可用',
          icon: 'none',
          duration: 3000
        })
      } else {
        wx.navigateTo ({
          url: '../vip/vip'
        })
      }
    } else { // 跳转去登录页面
      wx.reLaunch({
        url: '/pages/wechat/wechat',
      })
    }
  },
  goMoney: function() {
    wx.navigateTo ({
      url: '../money/money'
    })
  },
  hidePhoneInfo: function(tel) { // 修剪电话号码
    var reg = /^(\d{3})\d{4}(\d{4})$/;
    tel = tel.replace(reg, "$1****$2");
    return tel
  },
  copyWebAddr: function(e){ //复制电脑端网址
    var addr = e.currentTarget.dataset.addr
    wx.setClipboardData({
      data: addr,
      success: function(res) {
        wx.showToast({
          title: '复制成功！',
        })
      }
    })
  },
  contactService: function(){ //联系客服
    wx.showModal({
      title: '客服电话',
      content: '暂无',
      showCancel: false,//是否显示取消按钮
    })
  },
  feedback: function(){ //意见反馈
    wx.navigateTo({
      url: '/pages/authorization/authorization',
    })
  }
})
